package com.planshare.activity;

import java.net.MalformedURLException;
import java.net.URL;

import org.json.rpc.client.HttpJsonRpcClientTransport;
import org.json.rpc.client.JsonRpcInvoker;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.planshare.object.AndroidPlan;
import com.planshare.service.UserService;
import com.planshare.util.UserUtil;

public class ViewPlanActivity extends Activity {

	private boolean isAttending = false;
	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_plan);

		TextView nameTextView = (TextView) findViewById(R.id.planName);
		TextView locationTextView = (TextView) findViewById(R.id.planLocation);
		TextView descriptionTextView = (TextView) findViewById(R.id.planDescription);
		button = (Button) findViewById(R.id.attendButton);
		nameTextView.setText(UserUtil.planViewed.getName());
		locationTextView.setText(UserUtil.planViewed.getLocation() + " - " + UserUtil.planViewed.getDate().toString());
		descriptionTextView.setText(UserUtil.planViewed.getDescription());

		if (UserUtil.userPlans == null) {
			button.setVisibility(View.INVISIBLE);
			setIsAttending(false);
		} else {
			boolean changed = false;
			for (AndroidPlan p : UserUtil.userPlans) {
				if (p.getId().equals(UserUtil.planViewed.getId())) {
					setIsAttending(true);
					changed = true;
					break;
				}
			}
			if (!changed) {
				setIsAttending(false);
			}
		}
	}

	public void attend(View view) {
		final ProgressDialog dialog = new ProgressDialog(ViewPlanActivity.this);
		dialog.setMessage("Changing status...");
		dialog.setIndeterminate(true);
		dialog.setCancelable(false);
		dialog.show();

		new Thread(new Runnable() {

			@Override
			public void run() {

				HttpJsonRpcClientTransport transport;
				try {
					transport = new HttpJsonRpcClientTransport(new URL(
							UserUtil.userServiceUrl));
					JsonRpcInvoker invoker = new JsonRpcInvoker();
					UserService calc = invoker.get(transport, "user",
							UserService.class);

					if (isAttending) {
						calc.deletePlanForUser(UserUtil.planViewed,
								UserUtil.user);
						AndroidPlan[] newPlans = new AndroidPlan[UserUtil.userPlans.length - 1];
						int i = 0;
						for (AndroidPlan p : UserUtil.userPlans) {
							if (!p.getId().equals(UserUtil.planViewed.getId())) {
								newPlans[i] = p;
								i++;
							}
						}
						UserUtil.userPlans = newPlans;
					} else {
						calc.createPlanForUser(UserUtil.planViewed,
								UserUtil.user);
						AndroidPlan[] newPlans = new AndroidPlan[UserUtil.userPlans.length + 1];
						int i = 0;
						for (AndroidPlan p : UserUtil.userPlans) {
							newPlans[i] = p;
							i++;
						}
						newPlans[UserUtil.userPlans.length] = UserUtil.planViewed;
						UserUtil.userPlans = newPlans;
					}

					ViewPlanActivity.this.runOnUiThread(new Runnable() {
						public void run() {
							setIsAttending(!isAttending);
							dialog.dismiss();
						}
					});
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();

	}

	private void setIsAttending(boolean attending) {
		if (attending) {
			button.setText("Attending");
		} else {
			button.setText("Attend");
		}

		isAttending = attending;
	}

}
